﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using OneMile.Entity;
using System.Data;
using OneMile.Entity.Model;

namespace OneMile.DAL
{
    public class DALSLogin : BaseDAL
    {
        public bool IsShopsExist(String name)
        {
            try
            {
                String sql = @"SELECT COUNT(1) FROM Fct_Shops WHERE LoginName=@name";
                List<SqlParameter> list = new List<SqlParameter>();
                list.Add(new SqlParameter("@name", name));
                int count = Convert.ToInt32(DBHelperSQL.sqlExecuteScalar(sql, list));
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                return false;
            }
        }

        /// <summary>
        /// 获取登陆商家信息，为空则登录失败
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public EShopsInfo ShopsLogin(String name, String pwd)
        {
            String sql = @"SELECT ShopsName,ShopsTel,ShopsId,LoginName,Password 
                             FROM Fct_Shops 
                            WHERE LoginName=@name AND Password=@pwd";
            EShopsInfo shopInfo = null;
            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, Con))
                {
                    cmd.Parameters.Add(new SqlParameter("@name", name));
                    cmd.Parameters.Add(new SqlParameter("@pwd", pwd));
                    DbOpen();
                    using (IDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            shopInfo = new EShopsInfo();
                            shopInfo.ShopsName = Convert.ToString(dr["ShopsName"]);
                            shopInfo.ShopsTel = Convert.ToString(dr["ShopsTel"]);
                            Guid id=new Guid(Convert.ToString(dr["ShopsId"]));
                            shopInfo.ShopsId =id;
                            shopInfo.LoginName = Convert.ToString(dr["LoginName"]);
                            shopInfo.Password = Convert.ToString(dr["Password"]);
                        }
                    }
                    return shopInfo;
                }
            }
            catch (Exception ex)
            {
                base.WriteLog("DALShopLogin_ShopsLogin_" + ex.Message);
                return null;
            }
            finally
            {
                DbClose();
            }
        }
    }
}